import random
import defaultvalue

class BlackBoard:
    """ where tanker communicate with others """
    def __init__(self):
        self.reset()

    def reset(self):
        self.chase_time = 0 # cancel chase action after this time
        self.attack_time = 0 # cancel attack action after this time
        self.chase_player = False
        self.attack_base = False

    def set_chaseplayer(self, chase_player):
        """ begin to chase player and reset chase time """
        self.chase_player = chase_player
        if chase_player == True:
            self.chase_time = defaultvalue.default_canceltime

    def set_attackbase(self, attack_base):
        """ begin to attack base and reset attack time """
        self.attack_base = attack_base
        if attack_base == True:
            self.attack_time = defaultvalue.default_canceltime

    def tanker_dead(self, tank):
        """ when a tanker dies, other tankers will revenge """
        if tank:
            index = random.uniform(0, 1)
            if index < 0.33:
                self.set_chaseplayer(True) # enemys around chase player
            elif index < 0.66:
                self.set_attackbase(True) # enemys around attack base
            else:
                # two actions happen together
                self.set_chaseplayer(True)
                self.set_attackbase(True)

    def player_dead(self, player):
        if player:
            self.reset()

    def get_status(self):
        """ get chase or attack status """
        # update chase time
        if self.chase_time > 0:
            self.chase_time -= 1
        if self.chase_time <= 0:
            self.chase_player = False
        # update attack time
        if self.attack_time > 0:
            self.attack_time -= 1
        if self.attack_time <= 0:
            self.attack_base = False            
        return self.chase_player,self.attack_base
